﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MinhUtils
{
    public static class FormUtil
    {
        /// <summary>
        /// Display a message box contains Yes and No buttons
        /// </summary>
        /// <param name="message">The message to be displayed in message box</param>
        public static bool Confirm(string message)
        {
            return (MessageBox.Show(message, "Confirm!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes);
        }

        /// <summary>
        /// Fill a ComboBox with (1-12) values which could be used for choosing a month .
        /// </summary>
        /// <param name="cbx">The ComboBox to be filled</param>
        /// <param name="addItemAll">'true' to add an "All" item at first</param>
        /// <param name="selectCurrentMonth">'true' to select the current month by default</param>
        public static void FillComboxBoxMonth(ComboBox cbx, bool addItemAll, bool selectCurrentMonth)
        {
            object defaultMonth = null;
            if (selectCurrentMonth) defaultMonth = DateTime.Today.Month;

            FillComboxBoxNumber(cbx, 1, 12, defaultMonth, addItemAll);
        }

        /// <summary>
        /// Fill a ComboxBox with numbers between minValue & maxValue
        /// </summary>
        /// <param name="cbx">The ComboBox to be filled</param>
        /// <param name="minValue">The smallest value to be filled</param>
        /// <param name="maxValue">The biggest value to be filled</param>
        /// <param name="defaultValue">The default value to be choose by default. Use 'null' for none.</param>
        /// <param name="addItemAll">'true' to add an "All" item at first</param>
        public static void FillComboxBoxNumber(ComboBox cbx, int minValue, int maxValue, object defaultValue, bool addItemAll)
        {
            cbx.Items.Clear();

            if(addItemAll)
                cbx.Items.Add("All");

            for (int i = minValue; i <= maxValue; i++)
            {
                cbx.Items.Add(i);

                if (defaultValue != null && (int)defaultValue == i)
                {
                    cbx.SelectedIndex = cbx.Items.Count - 1;
                }
            }
        }

        /// <summary>
        /// Show user an Error Message using MessageBox
        /// </summary>
        public static void ShowErrorMessage(string errorMessage)
        {
            MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// Select all text in the TextboxBase control whenver user press Ctrl+A 
        /// </summary>
        public static void HandleControlA(TextBoxBase textBoxBase, KeyEventArgs e)
        {
            //http://www.eggheadcafe.com/software/aspnet/30475624/select-all-ctrla-does-not-work-in-textbox.aspx
            if ((Control.ModifierKeys == Keys.Control))
            {
                if(e.KeyCode == Keys.A)
                {
                    textBoxBase.SelectAll();
                }
            }
        }

        /// <summary>
        /// Select all text in the ComboBox control whenver user press Ctrl+A 
        /// </summary>
        public static void HandleControlA(ComboBox comboBox, KeyEventArgs e)
        {
            if ((Control.ModifierKeys == Keys.Control))
            {
                if (e.KeyCode == Keys.A)
                {
                    comboBox.SelectAll();
                }
            }
        }

        public static void TextBoxControlAHanlder(object sender, KeyEventArgs e)
        {
            if (sender is TextBox)
            {
                HandleControlA(sender as TextBox, e);
            }
        }

        /// <summary>
        /// Show a dialog for user to browse to a folder. User's selected path will be displayed in the given textbox
        /// </summary>
        /// <param name="txtFolder">Textbox to display user's selected folder path</param>
        public static void ShowFolderBrowser(TextBox txtFolder)
        {
            FolderBrowserDialog fbDialog = new FolderBrowserDialog();
            fbDialog.RootFolder = Environment.SpecialFolder.Desktop;

            if (fbDialog.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = fbDialog.SelectedPath;
            }
        }

        /// <summary>
        /// Set a link of a LinkLabel
        /// </summary>
        public static void SetLink(LinkLabel linkLabel, string url)
        {
            if (linkLabel.Links.Count > 0)
                linkLabel.Links.Remove(linkLabel.Links[0]);
            linkLabel.Links.Add(0, linkLabel.Text.Length, url);
        }

        public static void OpenLink(LinkLabel.Link link)
        {
            if (string.IsNullOrEmpty(link.LinkData.ToString()) == false)
            {
                //http://support.microsoft.com/kb/320478
                System.Diagnostics.ProcessStartInfo sInfo = new System.Diagnostics.ProcessStartInfo(link.LinkData.ToString());
                System.Diagnostics.Process.Start(sInfo);
            }
        }

        public static void SetLabelText(Label label, string text)
        {
            label.Text = text;
        }

        public static void EnableControl(Control control, bool enabled)
        {
            control.Enabled = enabled;
        }

        public static void CheckAll(CheckedListBox checkListBox)
        {
            CheckAll(checkListBox, true);
        }

        public static void UncheckAll(CheckedListBox checkListBox)
        {
            CheckAll(checkListBox, false);
        }

        /// <summary>
        /// Check or uncheck all chapters in CheckboxList
        /// </summary>
        /// <param name="value">true to check all, false to uncheck all</param>
        public static void CheckAll(CheckedListBox checkListBox, bool value)
        {
            if (value == true) //check all
            {
                //if not all chapters are already checked
                if (checkListBox.CheckedItems.Count != checkListBox.Items.Count)
                {
                    //select all chapters
                    checkListBox.BeginUpdate();
                    for (int i = 0; i < checkListBox.Items.Count; i++)
                    {
                        checkListBox.SetItemChecked(i, true);
                    }
                    checkListBox.EndUpdate();
                }
            }
            else //uncheck all
            {
                //if not all chapters are already unchecked
                if (checkListBox.CheckedItems.Count != 0)
                {
                    //unselect all chapters
                    checkListBox.BeginUpdate();
                    for (int i = 0; i < checkListBox.Items.Count; i++)
                    {
                        checkListBox.SetItemChecked(i, false);
                    }
                    checkListBox.EndUpdate();
                }
            }
        }
    }
}
